/**
 * @file recentrecord.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_RECORD!= 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static lv_obj_t * record_list_add(lv_obj_t * list, app_adaptor_pedometer_record_t * record);
static void recentrecord_init(app_adaptor_pedometer_record_t * record);
static lv_res_t record_list_scrl_signal_cb(lv_obj_t * scrl, lv_signal_t sign, void * param);
extern char * gcvt(double value, int32_t ndigit, char * buf);
/**********************
 *  STATIC VARIABLES
 **********************/
static lv_signal_cb_t orignal_list_scrl_signal_cb;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * record_create(lv_obj_t * tileview, uint16_t id)
{
    uint8_t i = 0;
    app_adaptor_pedometer_record_t * record = NULL;

    record = lv_mem_alloc(PEDOMETER_RECORD_LEN);
    LV_ASSERT_MEM(record);
    memset(record, 0, PEDOMETER_RECORD_LEN);
    recentrecord_init(record);

    lv_obj_t * tab = launcher_tileview_add_tile(tileview, id);
    LV_ASSERT_MEM(tab);
    if(tab == NULL) return NULL;

    /* The obj in only for reserve attribute in ext. */
    lv_obj_t * obj = lv_obj_create(tab, NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_obj_set_size(obj, lv_obj_get_width(tab), lv_obj_get_height(tab));
    lv_obj_add_style(obj, LV_OBJ_PART_MAIN, &lv_style_transp_tight);
    lv_tileview_add_element(tileview, obj);
    lv_obj_set_click(obj, false);

    lv_obj_align(obj, tab, LV_ALIGN_CENTER, 0 , 0);
    lv_obj_t * content = lv_cont_create(obj, NULL);
    lv_obj_add_style(content, LV_CONT_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_size(content, LV_HOR_RES, LV_VER_RES);
    lv_cont_set_layout(content, LV_LAYOUT_COLUMN_MID);
    lv_tileview_add_element(tileview, content);
    lv_obj_set_click(content, false);

    lv_obj_t * title = lv_label_create(content, NULL);
    lv_obj_set_size(title, LV_HOR_RES, LV_VER_RES / 4);
    lv_label_set_text_id(title, WATCH_TEXT_ID_RECENT_RECORD);
    lv_obj_add_style(title, LV_LABEL_PART_MAIN, &lv_watch_font30_gray);
    lv_tileview_add_element(tileview, title);

    lv_obj_t * list = lv_list_create(content, NULL);
    lv_obj_set_size(list, LV_HOR_RES, LV_VER_RES * 3 / 4);
    lv_page_set_edge_flash(list, false);
    lv_page_set_scrollbar_mode(list, LV_SCROLLBAR_MODE_OFF);
    orignal_list_scrl_signal_cb = lv_obj_get_signal_cb(lv_page_get_scrl(list));
    lv_obj_set_signal_cb(lv_page_get_scrl(list), record_list_scrl_signal_cb);
    lv_tileview_add_element(tileview, list);
    lv_tileview_add_element(tileview, lv_page_get_scrl(list));

    /* update list style*/
    lv_obj_add_style(list, LV_LIST_PART_BG, &lv_watch_style_transp);
    lv_obj_add_style(list, LV_LIST_PART_SCROLLBAR, &lv_watch_cont_opa1);
    lv_obj_set_style_local_bg_opa(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_style_local_pad_inner(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, 10);
    lv_obj_set_style_local_pad_left(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, 5);
    lv_obj_set_style_local_pad_right(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, 5);

    for(i = 0; i < PEDOMETER_MAX_RECORD_COUNT; i++) {
        record_list_add(list, &(record[i]));
    }
    lv_mem_free(record);
    return tab;
}

void app_adaptor_update_pedometer_record_ind(app_adaptor_pedometer_record_t * record)
{
    uint8_t i = 0;
    lv_obj_t * activity_obj  = lv_watch_get_activity_obj(ACT_ID_PEDOMETER);
    if(activity_obj == NULL) {
        return;
    }

    lv_obj_t * tabview = lv_obj_get_child(lv_obj_get_child(activity_obj, NULL), NULL);
    lv_obj_t * list = lv_obj_get_child(lv_obj_get_child(lv_obj_get_child(lv_watch_tabview_get_tab(tabview, 2), NULL), NULL), NULL);
    lv_list_clean(list);

    for(i = 0; i < PEDOMETER_MAX_RECORD_COUNT; i++) {
        record_list_add(list, &record[i]);
    }
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void recentrecord_init(app_adaptor_pedometer_record_t * record)
{
    //temp
    uint8_t i = 0;
    Hal_Rtc_Gettime(&(record[0].date));
    record[0].step = pedometer_get_steps();
    record[0].kcal = record[0].step / 40;
    record[0].km = record[0].step / (2 * 1000) ;

    for(i = 1; i < PEDOMETER_MAX_RECORD_COUNT; i++) {
        record[i].date.tm_year = 2019;
        record[i].date.tm_mon = 7;
        record[i].date.tm_mday = PEDOMETER_MAX_RECORD_COUNT - i;
        record[i].step = 5000 + 300 * i;
        record[i].kcal = (float)(record[i].step) / 40;
        record[i].km = (float)(record[i].step) / (2 * 1000);
    }
}

static lv_res_t record_list_scrl_signal_cb(lv_obj_t * scrl, lv_signal_t sign, void * param)
{
    lv_drag_dir_t drag_dir;
    lv_indev_t * indev = lv_indev_get_act();
    static lv_point_t ori_pt;
    lv_point_t act_pt;
    if(LV_SIGNAL_PRESSED == sign) {
        ori_pt.y = indev->proc.types.pointer.act_point.y;
        return LV_RES_OK;
    } else if (LV_SIGNAL_PRESSING == sign) {
        drag_dir = indev->proc.types.pointer.drag_dir;
        if(drag_dir & LV_DRAG_DIR_VER) {
            lv_indev_get_point(indev, &act_pt);
            if(act_pt.y - ori_pt.y < -1){
                lv_obj_set_y(scrl, lv_obj_get_y(scrl) + (act_pt.y - ori_pt.y));
            }
            else if(act_pt.y - ori_pt.y > 1) {
                if(lv_obj_get_y(scrl) < -1) {
                    if(lv_obj_get_y(scrl) + (act_pt.y - ori_pt.y) > 0)
                        lv_obj_set_y(scrl, 2);
                    else
                        lv_obj_set_y(scrl, lv_obj_get_y(scrl) + (act_pt.y - ori_pt.y));
                }  
            }
            ori_pt.y = act_pt.y;
        }
        return LV_RES_OK;
    }
    return orignal_list_scrl_signal_cb(scrl, sign, param);
}

static lv_obj_t * record_list_add(lv_obj_t * list, app_adaptor_pedometer_record_t * record)
{
    char str[16] = {0};
    static uint16_t btn_size = 0;
    btn_size ++;
    /*Create a list element with the image an the text*/
    lv_obj_t * btn;
    btn = lv_btn_create(list, NULL);

    /*Save the original signal function because it will be required in `lv_list_btn_signal`*/
    lv_list_set_ancestor_btn_signal(btn);

    /*Set the default styles*/
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_set_style_local_bg_opa(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_20);
    lv_obj_set_style_local_radius(btn, LV_BTN_PART_MAIN,  LV_STATE_DEFAULT, 15);
    lv_obj_set_style_local_pad_left(btn,  LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 10);
    lv_obj_set_style_local_pad_right(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 10);
    lv_obj_set_style_local_pad_top(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_bottom(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_inner(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 20);
    lv_obj_set_style_local_line_width(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_COLOR_WHITE);
    lv_obj_set_style_local_bg_opa(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_OPA_20);
    lv_obj_set_style_local_radius(btn, LV_BTN_PART_MAIN,  LV_STATE_PRESSED, 15);
    lv_obj_set_style_local_pad_left(btn,  LV_BTN_PART_MAIN, LV_STATE_PRESSED, 10);
    lv_obj_set_style_local_pad_right(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 10);
    lv_obj_set_style_local_pad_top(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 0);
    lv_obj_set_style_local_pad_bottom(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 0);
    lv_obj_set_style_local_pad_inner(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 20);
    lv_obj_set_style_local_line_width(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, 0);

    lv_page_glue_obj(btn, true);
    lv_obj_add_protect(btn, LV_PROTECT_PRESS_LOST);
    lv_obj_set_signal_cb(btn, lv_list_get_listbtn_signal());
    lv_obj_set_click(btn, false);

    /*Make the size adjustment*/
    lv_coord_t w = lv_obj_get_width(list);
    uint8_t pad_w = w - lv_obj_get_style_pad_left(list, LV_LIST_PART_SCROLLABLE);
    lv_obj_set_size(btn, pad_w, LV_VER_RES / 4);
    lv_btn_set_layout(btn, LV_LAYOUT_ROW_MID);
    

    lv_obj_t * cont1 = lv_cont_create(btn, NULL);
    lv_obj_set_style_local_bg_opa(cont1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_click(cont1, false);
    lv_cont_set_fit2(cont1, LV_FIT_TIGHT, LV_FIT_TIGHT);
    lv_obj_set_style_local_pad_top(cont1, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_inner(cont1, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
    if(lv_obj_get_base_dir(btn) == LV_BIDI_DIR_RTL)
        lv_cont_set_layout(cont1, LV_LAYOUT_COLUMN_RIGHT);
    else
        lv_cont_set_layout(cont1, LV_LAYOUT_COLUMN_LEFT);

    /*date*/
    lv_obj_t * label_date = lv_label_create(cont1, NULL);
    lv_label_set_long_mode(label_date, LV_LABEL_LONG_SROLL);
    lv_obj_set_size(label_date, w * 2 / 5, LV_VER_RES / 8);
    lv_obj_set_pos(label_date, 10, 5);
    lv_obj_add_style(label_date, LV_LABEL_PART_MAIN, &lv_watch_font14);
    if
    (btn_size == 1) {
        lv_label_set_text(label_date, "Today");
    } else {
        memset(str, 0, 16);
        snprintf(str, 16, "%d.%02d.%02d", record->date.tm_year, record->date.tm_mon, record->date.tm_mday);
        lv_label_set_text(label_date, str);
    }
    lv_obj_set_click(label_date, false);



    /*steps*/
    lv_obj_t * cont_step = lv_cont_create(cont1, cont1);
    lv_obj_set_style_local_pad_inner(cont_step, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5);
    lv_cont_set_layout(cont_step, LV_LAYOUT_ROW_MID);

    lv_obj_t * img_step = lv_img_create(cont_step, NULL);
    lv_img_set_src(img_step, ICON_STEP);
    lv_obj_set_click(img_step, false);

    lv_obj_t * label_step = lv_label_create(cont_step, NULL);
    memset(str, 0, 16);
    snprintf(str, 16, "%d", record->step);
    strcat(str, lv_lang_get_text(WATCH_TEXT_ID_STEPS));
    lv_label_set_text(label_step, str);
    lv_obj_add_style(label_step, LV_LABEL_PART_MAIN,  &lv_watch_font20_yellow);
    lv_obj_set_click(label_step, false);

     lv_obj_t * cont2 = lv_cont_create(btn, cont1);
    /*calrolies*/

    lv_obj_t * cont_cal = lv_cont_create(cont2, cont_step);
    lv_obj_t * img_cal = lv_img_create(cont_cal, NULL);
    lv_img_set_src(img_cal, ICON_CAL);
    lv_obj_set_click(img_cal, false);
    lv_obj_t * label_cal = lv_label_create(cont_cal, label_date);
    memset(str, 0, 16);
    gcvt(record->kcal, 4, str);
    strcat(str, lv_lang_get_text(WATCH_TEXT_ID_KCALORIES));
    lv_label_set_text(label_cal, str);
    lv_obj_set_click(label_cal, false);
    lv_obj_add_style(label_cal, LV_LABEL_PART_MAIN, &lv_watch_font20);

    /*distance*/

    lv_obj_t * cont_dis = lv_cont_create(cont2, cont_step);
    lv_obj_t * img_dis = lv_img_create(cont_dis, NULL);
    lv_img_set_src(img_dis, ICON_DISTANCE);
    lv_obj_set_click(img_dis, false);
    lv_obj_t * label_dis = lv_label_create(cont_dis, NULL);
    memset(str, 0, 16);
    gcvt(record->km, 3, str);
    strcat(str, lv_lang_get_text(WATCH_TEXT_ID_KMETERS));
    lv_label_set_text(label_dis, str);
    lv_obj_set_click(label_dis, false);
    lv_obj_add_style(label_dis, LV_LABEL_PART_MAIN, &lv_watch_font20);

    return btn;
}

#endif /*USE_LV_WATCH_RECORD*/
